package com.yd.mvc.pc.agreementController;

import com.yd.app.service.pc.agreementService.AgreementService;
import com.yd.app.util.BusinessException;
import com.yd.domain.business.Agreement;
import com.yd.domain.commom.PageModel;
import com.yd.domain.sys.DicEnum;
import com.yd.mvc.baseController.BaseController;
import com.yd.mvc.baseController.util.AuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * Created by sunxianhui on 2018/3/9.
 */
@RestController
@RequestMapping(value = "/api/agreement")
public class AgreementController extends BaseController {
    @Autowired
    AgreementService agreementService;

    /**
     * 添加
     * @param agreement
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public void addAgreement(@RequestBody Agreement agreement) throws BusinessException {
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        if(user!=null){
            getIfHasPerm(AuthUtil.getCurrentUser(), DicEnum.PERM_CODE.AGREEMENRT_MANAGER);//验证权限
        }
         agreementService.addAgreement(agreement);
    }

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    public Agreement getAgreement(@PathVariable("id")String id) throws BusinessException {
        Agreement agreement= null;
        try {
            agreement = agreementService.getAgreementDetail(id);
        } catch (Exception e) {
            throw new BusinessException(500,"获取协议信息异常");
        }
        return agreement;
    }
    /**
     * 修改
     * @param agreement
     */
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public void updateAgreement(@RequestBody Agreement agreement) throws BusinessException {
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        if(user!=null){
            getIfHasPerm(AuthUtil.getCurrentUser(), DicEnum.PERM_CODE.AGREEMENRT_MANAGER);//验证权限
        }
        try {
            agreementService.updateAgreement(agreement);
        } catch (Exception e) {
            throw new BusinessException(500, "修改协议信息失败");
        }
    }
    /**
     * 删除
     * @param id
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public void deleteAgreement(@PathVariable("id") String id) throws BusinessException {
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        if(user!=null){
            getIfHasPerm(AuthUtil.getCurrentUser(), DicEnum.PERM_CODE.AGREEMENRT_MANAGER);//验证权限
        }
        agreementService.deleteAgreementById(id);
    }

    /**
     * 分页查询
     * @param agreement
     * @param pageIndex
     * @param pageSize
     * @return
     * @throws BusinessException
     */
    @RequestMapping(value = "/getList/{pageIndex}/{pageSize}", method = RequestMethod.POST)
    public PageModel getAgreementList(@RequestBody Agreement agreement, @PathVariable("pageIndex") int pageIndex, @PathVariable("pageSize") int pageSize) throws BusinessException {
        PageModel pageModel = null;
        try {
            pageModel = agreementService.getAgreementList(agreement, pageIndex, pageSize);
        } catch (Exception e) {
            throw new BusinessException(500, "获取协议信息失败");
        }
        return pageModel;
    }

    /**
     * 查询协议数据列表
     * @return
     * @throws BusinessException
     */
    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    public List<Agreement> getAgreementList() throws BusinessException {
        List<Agreement> list=new ArrayList<>();
        try {
            list = agreementService.getAgreementList();
        } catch (Exception e) {
            throw new BusinessException(500, "获取协议信息失败");
        }
        return list;
    }
}
